#coding=utf8
import xlrd



class ReadExcel(object):

    def __init__(self, excelPath, sheetName):
        self.data = xlrd.open_workbook(excelPath)
        if sheetName in self.data.sheet_names():
            self.table = self.data.sheet_by_name(sheetName)
            # 获取第一行作为key值
            self.keys = self.table.row_values(0)
            # 获取总行数
            self.rowNum = self.table.nrows
            # 获取总列数
            self.colNum = self.table.ncols

    def dict_data(self):
        try:
            if self.rowNum <= 1:
                print('总行数小于1，请确认Excel中是否存在测试数据')
            else:
                resultList = []
                j = 1
                for i in range(self.rowNum-1):
                    s = {}
                    # 从第二行开始取数据
                    values = self.table.row_values(j)
                    for x in range(self.colNum):
                        s[self.keys[x]] = values[x]
                    resultList.append(s)
                    j += 1
                return resultList
        except:
            return []

    # 过滤出对应test_xxx.py中的方法对应的用例
    @staticmethod
    def filter(modelName, methodName, dataBeforeFilter):
        resultList = dataBeforeFilter
        tempList = []
        for dic in resultList:
            if dic["test_xx.py"] == modelName and dic["yes/no"] == "yes" and dic["functionInmodel"] == methodName:
                tempList.append(dic)
        return tempList


